import dataUrl from '@/utils/request/config.js'
import store from '@/utils/store/index.js'
import md5 from '@/utils/encrypt/md5.js'
import config from "@/utils/request/config.js"

function checkStatus(response) {
	uni.hideLoading()
	if (response[1]) {
		if (response[1].statusCode === 200 || response[1].statusCode === 304) {
			return response[1].data
		} else {
			console.log(JSON.stringify(response));
			return {
				status: 500,
				info: '网络异常 请稍后再试~'
			}
		}
	} else {
		console.log(JSON.stringify(response));
		return {
			status: 500,
			info: '网络异常 请稍后再试~~'
		}
	}
}

function checkCode(res) {
	if (config.patternDevelopment) { //开发者模式打开日志功能
		console.log("返回结果:" + JSON.stringify(res));
	}

	if (res.status === 403) {
		uni.removeStorageSync("user");
		uni.reLaunch({
			url: '/pages/wallet/create-wallet'
		})
	}
	if (res.status === 405) {
		uni.navigateTo({
			url: '/pages/version'
		})
	}
	if (res.status != 1) {
		uni.showToast({
			title: res.info,
			icon: 'none',
			duration: 2000
		})
		return res
	} else {
		return res
	}
}
/**
 * 为H5平台生成device信息（imei\version\appid\device
 */
function createDeviceInfoForH5() {
	var imei, appid, device = 'h5';
	imei = uni.getStorageSync("imei");
	if (imei == null || imei == "" || typeof(imei) == "undefined") {
		imei = Math.floor(Math.random() * 100000000);
		uni.setStorageSync("imei", imei);
	}
	var deviceInfo = {
		imei: imei,
		appid: 'browser',
		device: device
	};
	return deviceInfo;

}

function getDeviceIMEI() {
	var imei;
	//#ifdef APP-PLUS
	imei = plus.device.imei != " " ? plus.device.imei : plus.device.uuid;
	//#endif
	//#ifndef APP-PLUS
	var deviceInfo = createDeviceInfoForH5();
	imei = deviceInfo.imei;
	//#endif
	return imei;
}
export default {
	getDeviceIMEI,
	getUser() {
		var user = null;
		user = uni.getStorageSync("user");
		return user;
	},
	post(url, data, header = null) {
		uni.showLoading({
			mask: true
		})
		var Authorization
		var user = uni.getStorageSync("user");
		Authorization = user.token;
		return uni.request({
			url: dataUrl.url + url,
			data: header ? JSON.stringify(data) : data,
			method: 'POST',
			header: {
				'Authorization': Authorization,
				'versionBuild': config.versionBuild,
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
				...header
			}
		}).then(checkStatus).then(checkCode)
	},
	postJson(url, data, header = null, loading = true) {
		//imei  nonce_str  version sign 附加参数
		let nonce = Math.floor(Math.random() * 100000000);
		let imei, version, appid, device;
		//app build version(eg. 10000)
		version = dataUrl.versionBuild;
		//#ifdef APP-PLUS
		imei = getDeviceIMEI();
		appid = plus.runtime.appid;
		device = plus.os.name;
		//#endif
		//#ifndef APP-PLUS
		let deviceInfo = createDeviceInfoForH5();
		imei = deviceInfo.imei;
		appid = deviceInfo.appid;
		device = deviceInfo.device;
		//#endif

		//补充封装data信息
		data.nonce = nonce;
		data.imei = imei;
		data.version = version;
		data.appid = appid;
		data.device = device;

		//签名
		data.sign = md5.makeSign(data, dataUrl.apiKey);
		if (config.patternDevelopment) { //开发者模式打开日志功能
			console.log("请求接口:" + dataUrl.url + url);
			console.log("请求参数:" + JSON.stringify(data))
		}

		if (loading) {
			uni.showLoading({
				mask: true
			})
		}

		var Authorization
		var user = uni.getStorageSync("user");

		if (config.patternDevelopment) {
			console.log("user=", JSON.stringify(user));
			console.log("token", user.token);
		}

		Authorization = user.token;
		return uni.request({
			url: dataUrl.url + url,
			data: header ? JSON.stringify(data) : data,
			method: 'POST',
			header: {
				'Authorization': Authorization,
				'versionBuild': config.versionBuild,
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
				...header
			}
		}).then(checkStatus).then(checkCode)
	},
	get(url, params, header) {
		var Authorization
		if (params) {
			let paramsArray = []
			Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key]))
			if (url.search(/\?/) === -1) {
				url += '?' + paramsArray.join('&')
			} else {
				url += '&' + paramsArray.join('&')
			}
		}
		var user = uni.getStorageSync("user");
		Authorization = user.token;
		return uni.request({
			url: dataUrl.url + url,
			data: header ? JSON.stringify(params) : params,
			method: 'GET',
			header: {
				'Authorization': Authorization,
				'versionBuild': config.versionBuild,
				...header
			}
		}).then(checkStatus).then(checkCode)
	}
}
